<?php
session_start();
require_once("database.php");
$conn=mysql_link();
$us=$_SESSION['email'];
$cha="SELECT borrowrecords.*,users.* FROM borrowrecords INNER JOIN users on borrowrecords.UserID = users.UserID where Email='$us'";
$sql2= mysqli_query($conn,$cha);
$res2= mysqli_fetch_assoc($sql2);
$username = $res2["Name"];
//echo $username;

if ($_GET["action"]=="jilu"){
    $data=$_POST['options'];
}
$d=explode("-",$data);
$year=$d[0];
$month=$d[1];
$sql="SELECT borrowrecords.*,users.* FROM borrowrecords INNER JOIN users on borrowrecords.UserID = users.UserID  where Name='$username' and re=1 and YEAR(BorrowDate)='$year' AND MONTH(BorrowDate)='$month'";
$sql1= mysqli_query($conn,$sql);
$res=mysqli_fetch_all($sql1,MYSQLI_ASSOC);

$sql3="SELECT borrowrecords.*,users.* FROM borrowrecords INNER JOIN users on borrowrecords.UserID = users.UserID where Name='$username' and re=1 and YEAR(BorrowDate)='$year' AND MONTH(BorrowDate)='$month'";
$sql3= mysqli_query($conn,$sql3);
$count = mysqli_num_rows($sql3);

$sql4="SELECT borrowrecords.*,users.* FROM borrowrecords INNER JOIN users on borrowrecords.UserID = users.UserID where Name='$username' and re=2 and YEAR(BorrowDate)='$year' AND MONTH(BorrowDate)='$month'";
$sql4= mysqli_query($conn,$sql4);
$count1 = mysqli_num_rows($sql4);


$ym="SELECT DISTINCT YEAR(BorrowDate) AS year,MONTH(BorrowDate) AS month FROM borrowrecords ORDER BY year,month;";
$ymsql= mysqli_query($conn,$ym);
$cuyear= mysqli_fetch_all($ymsql,MYSQLI_ASSOC);
?>
<!DOCTYPE html>
<html lang="zh">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>图书馆图书目录</title>  
    <link rel="stylesheet" href="../css/借阅记录.css">
</head>  
<body> 
    <div class="borrow_records">
        <div class="top">
            <div class="gang"></div>
            <h2>借阅记录</h2>
        </div>
        <div class="middle">
            <div class="middle-left">
                <div class="borrow"><h1><?echo $count?></h1></div>
                <div><p>借读（本）</p></div>
            </div>
            <div class="middle-right">
                <div class="return"><h1><?echo $count1?></h1></div>
                <div><p>归还（本）</p></div>
            </div>
        </div>
        <form action="?action=jilu" id="form" method="post">
        <div class="date">
            <select name="options"  class="date-1" id="mySelect" required>
                <?foreach ($cuyear as $value){?>
                    <option value="<?echo $value['year'].'-'.$value['month']?>"><?echo $value['year'].'-'.$value['month']?></option>
                <?}?>
            </select>
            <input type="submit" value="查询">
<!--            提交表单后不重置的代码-->
            <script>
                document.getElementById("mySelect").addEventListener("change", function() {
                    var selectedOption = this.value;
                    localStorage.setItem("selectedOption", selectedOption);
                });

                window.addEventListener("load", function() {
                    var selectedOption = localStorage.getItem("selectedOption");
                    if (selectedOption) {
                        document.getElementById("mySelect").value = selectedOption;
                    }
                });

                document.getElementById("resetButton").addEventListener("click", function() {
                    localStorage.removeItem("selectedOption");
                });

            </script>

        </div>
        </form>
        <div class="records">
            <table>  
                <thead>  
                    <tr>  
                        <th>索书号</th>  
                        <th>条形码</th>  
                        <th>书籍</th>  
                        <th>文献所属地</th>  
                        <th>借阅时间</th>  
                        <th>应还时间</th>  
                    </tr>  
                </thead>  
                <tbody>
                <?foreach ($res as $value){?>
                    <tr>
                        <th><?echo $value['RecordID']?></th>
                        <th><?echo $value['BookID']?></th>
                        <th><?echo $value['Bookname']?></th>
                        <th><?echo $value['di']?></th>
                        <th><?echo $value['BorrowDate']?></th>
                        <th><?echo $value['ReturnDate']?></th>
                    </tr> 
                </tbody>
                <?}?>

            </table>  
        </div>
        <div class="last"><p>没有更多咯~</p></div>
    </div>
</body>  
</html>